//import org.locationtech.proj4j.*;
//
//public class CoordinateTransformExample {
//    public static void main(String[] args) {
//        // 定义要转换的坐标（经度、纬度）
//        ProjCoordinate sourceCoord = new ProjCoordinate(120.0, 30.0);
//        // 示例坐标
//        ProjCoordinate destCoord = new ProjCoordinate();
//        // 创建投影坐标系
//        CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84;
//        // WGS84坐标系
//        CoordinateReferenceSystem targetCRS = createTargetCRS();
//        // 自定义目标坐标系
//        // 转换坐标
//        try {
//            CoordinateTransform transform = new BasicCoordinateTransform(sourceCRS, targetCRS);
//            transform.transform(sourceCoord, destCoord);
//            System.out.println("转换后的坐标: " + destCoord.x + ", " + destCoord.y);
//        } catch (Proj4jException e){
//            System.err.println("坐标转换失败: " + e.getMessage());
//        }
//    }
//
//    private static CoordinateReferenceSystem createTargetCRS() {
//        // 此函数创建一个目标坐标系，具体实现依赖于你的需求 //
//        //TODO: Add actual target CRS creation logic based on your requirements
//        CRSFactory crsFactory = new CRSFactory();
//        return crsFactory.createFromName("EPSG:3857");
//        // 返回null，说明需要实现具体的目标坐标系
//    }
//}



import org.locationtech.proj4j.*;

public class Main {
    // 定义要转换的坐标（经度、纬度）
    ProjCoordinate sourceCoord = new ProjCoordinate(120.0, 30.0);
    // 示例坐标
    ProjCoordinate destCoord = new ProjCoordinate();
    // 创建投影坐标系
    CoordinateReferenceSystem sourceCRS;
    // WGS84坐标系
    CoordinateReferenceSystem targetCRS = createTargetCRS();

    public Main() {
        // 初始化sourceCRS为WGS84
        this.sourceCRS = createWGS84CRS();
    }

    public static void main(String[] args) {
        Main mainInstance = new Main();
        // 转换坐标
        try {
            CoordinateTransform transform = new BasicCoordinateTransform(mainInstance.sourceCRS, mainInstance.targetCRS);
            transform.transform(mainInstance.sourceCoord, mainInstance.destCoord);
            System.out.println("转换后的坐标: " + mainInstance.destCoord.x + ", " + mainInstance.destCoord.y);
        } catch (Proj4jException e) {
            System.err.println("坐标转换失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static CoordinateReferenceSystem createWGS84CRS() {
        // 使用参数创建WGS84地理坐标系
        CRSFactory crsFactory = new CRSFactory();
        return crsFactory.createFromParameters("WGS84", "+proj=longlat +datum=WGS84 +no_defs");
    }

    private static CoordinateReferenceSystem createTargetCRS() {
        // 使用参数创建Web Mercator投影坐标系 (EPSG:3857)
        CRSFactory crsFactory = new CRSFactory();
        return crsFactory.createFromParameters("EPSG:3857", "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs");
    }
}


